<?php
// 收款账户控制器
class Controller_WWW_Bank extends Controller_WWW_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:www/bank/create";
        parent::__construct($req, $res);
    }

    public function action_index ()
    {
        $this->request->redirect('/bank/create');
    }

    // 创建
    public function action_create ()
    {
        $post = $this->_post;
        if (empty($post))
        {
            $this->template->set_filename('smarty:www/bank/create');
        }
        else
        {
            $uid      = $this->_uid;
            $type     = trim(Arr::get($post, 'accountType'));
            $name     = trim(Arr::get($post, 'accountName'));
            $userName = trim(Arr::get($post, 'accountUserName'));

            $bank  = new UserExt_Bank($this->_uid);
            $bank->addBank($type, $name, $userName);

            if ($this->request->is_ajax())
            {
                echo json_encode(array('status' => 0));exit;
            }
            else
            {
                echo json_encode(array('status' => 1));exit;
                $banks = $bank->getBankList();
                $this->template->set_filename('smarty:www/bank/list');
                $this->template->set('bankList', $banks);
            }
        }
    }

    public function action_list ()
    {
        $bank  = new UserExt_Bank($this->_uid);
        $banks = $bank->getBankList();

        if ($this->request->is_ajax())
        {
            $html = View::factory('smarty:www/bank/list')->set('bankList', $banks)->render();
            echo json_encode(array('status' => 0, 'html' => $html));
            exit;
        }
        else
        {
            $this->template->set_filename('smarty:www/bank/list');
            $this->template->set('bankList', $banks);
        }
    }

    public function action_setDefault ($id)
    {
        $bank  = new UserExt_Bank($this->_uid);
        $bank->setDefault($id);
        $this->request->redirect('/guider/bank_list');
    }
    public function action_deleteBank ($id)
    {
        $bank  = new UserExt_Bank($this->_uid);
        $bank->deleteBank($id);
        $this->request->redirect('/guider/bank_list');
    }
}
